﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-06-08 16:43:20
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region 评论信息 数据访问层
	/// <summary>
	///评论信息 数据访问层
	/// </summary>	
	internal partial class Comment:IComment
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(CommentID),0)+1
                               FROM [Comment]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private CommentInfo GetByReader(IDataReader dr)
         {
            CommentInfo info = new CommentInfo();
            object obj;
            obj=dr["CommentID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CommentID = (long)obj;
            }
            obj=dr["CommentUserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CommentUserID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["CommentType"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CommentType = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["ContentID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ContentID = (long)obj;
            }
            obj=dr["Content"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Content = obj.ToString();
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            return info;
         }
		public long Insert(CommentInfo info)
		{
        
        
            string strSql=@"INSERT INTO Comment			
			(			
			[CommentUserID],
			[CommentType],
			[ContentID],
			[Content],
			[IsActive],
			[CreatedDate]
			)
			 VALUES
			(			
			@CommentUserID,
			@CommentType,
			@ContentID,
			@Content,
			@IsActive,
			@CreatedDate
			)
			 SET @CommentID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@CommentID",DbType.Int64, sizeof(Int64));  
            db.AddInParameter(dbc, "@CommentUserID",DbType.Int32, info.CommentUserID);
            db.AddInParameter(dbc, "@CommentType",DbType.Int32, info.CommentType);
            db.AddInParameter(dbc, "@ContentID",DbType.Int64, info.ContentID);
            db.AddInParameter(dbc, "@Content",DbType.String, info.Content);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (long)db.GetParameterValue(dbc,"CommentID");
         
		}
		
		
        public void Update(CommentInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE Comment SET ");
            sbSql.AppendLine("[CommentUserID] = @CommentUserID,");
                
            sbSql.AppendLine("[CommentType] = @CommentType,");
                
            sbSql.AppendLine("[ContentID] = @ContentID,");
                
            sbSql.AppendLine("[Content] = @Content,");
                
            sbSql.AppendLine("[IsActive] = @IsActive,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate");
                
            sbSql.Append(" where  [CommentID] = @CommentID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@CommentID",DbType.Int64, info.CommentID);
            db.AddInParameter(dbc, "@CommentUserID",DbType.Int32, info.CommentUserID);
            db.AddInParameter(dbc, "@CommentType",DbType.Int32, info.CommentType);
            db.AddInParameter(dbc, "@ContentID",DbType.Int64, info.ContentID);
            db.AddInParameter(dbc, "@Content",DbType.String, info.Content);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(long commentID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [Comment]  ");
            strSql.Append("WHERE [CommentID]=@CommentID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@CommentID",DbType.Int64, commentID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<CommentInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<CommentInfo> list = new List<CommentInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "Comment");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    CommentInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public CommentInfo GetByID(long commentID)
		{
            Model.CommentInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [Comment] ");
            strSql.Append("WHERE [CommentID]=@CommentID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@CommentID",DbType.Int64, commentID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}